package com.sawyerqiu.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.listener.ReadListener;
import com.sawyerqiu.constant.FileConstant;
import com.sawyerqiu.model.IntNo;

import java.io.File;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * @author : SawyerQiu
 * @date : 2024/7/22 11:19
 */
public class ExcelUtil {

    public static <T> void readDo(String excelPath, Class<T> clazz, Predicate<T> predicate, Consumer<T> consumer) {
        readDo(excelPath, null, clazz, predicate, consumer);
    }

    public static <T> void readDo(String excelPath, String sheetName, Class<T> clazz, Predicate<T> predicate, Consumer<T> consumer) {
        ExcelReaderBuilder excelReaderBuilder = EasyExcel.read(excelPath, clazz, new ReadListener<T>() {
            @Override
            public void invoke(T data, AnalysisContext analysisContext) {
                if (!predicate.test(data)) {
                    return;
                }

                consumer.accept(data);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
            }
        });
        if (sheetName == null || sheetName.equals("")) {
            excelReaderBuilder.sheet().doRead();
            return;
        }

        excelReaderBuilder.sheet(sheetName).doRead();
    }

    public static <T> void writeAppend(String excelPath, String sheerName, Class<T> clazz, List<T> datum) throws Exception {
        //原始excel
        File file = new File(excelPath);

        if (file.exists()) {
            // try reset no
            if (IntNo.class.isAssignableFrom(clazz)) {
                final int[] maxNo = {0};
                readDo(excelPath, sheerName, clazz, data -> true,
                        data -> {
                            IntNo intNo = (IntNo) data;
                            if (intNo.getNo() > maxNo[0]) {
                                maxNo[0] = intNo.getNo();
                            }
                        });

                for (int i = 0; i < datum.size(); i++) {
                    IntNo intNo = (IntNo) datum.get(i);
                    intNo.setNo(maxNo[0] + i + 1);
                }
            }

            String copy = excelPath.replace(FileConstant.EXCEL_FILE_SUFFIXES, "(备份)" + FileConstant.EXCEL_FILE_SUFFIXES);
            File copyFile = new File(copy);
            if (copyFile.exists()) {
                boolean delete = copyFile.delete();
                if (!delete) {
                    throw new Exception("删除备份文件失败：" + excelPath);
                }
            }

            //新excel
            String replacePath = excelPath.replace(FileConstant.EXCEL_FILE_SUFFIXES, "0" + FileConstant.EXCEL_FILE_SUFFIXES);
            File newFile = new File(replacePath);

            EasyExcel.write(file, clazz)
                    .needHead(false).
                    withTemplate(file).file(newFile).sheet(sheerName).doWrite(datum);

            file.renameTo(copyFile);
            newFile.renameTo(new File(excelPath));

        } else {
            throw new Exception("没找到对应文件：" + excelPath);

        }
    }
}
